package com.hancheng.commoncloud.framework.config;

import com.alibaba.druid.pool.DruidDataSource;
import io.seata.rm.datasource.DataSourceProxy;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * 通用 Seata 数据源代理配置类，供多个服务模块使用
 */
@Configuration
public class SeataDataSourceProxyConfig {

    @Bean
    @Primary
    public DataSource dataSource(DataSourceProperties dataSourceProperties) {
        DruidDataSource druidDataSource = dataSourceProperties
                .initializeDataSourceBuilder()
                .type(DruidDataSource.class)
                .build();
        return new DataSourceProxy(druidDataSource);
    }
}
